<template>
  <div class="home-page white-bg">
    <div class="user-wrapper">
      <div class="user-info">
        <img :src="avatar_url" class="user-avatar" />
        <div class="info-box">
          <div class="info-title">早上好，{{ userRealName }}</div>
          <div class="info-desc">欢迎使用，伏羲東方数字化智慧云平台</div>
        </div>
      </div>
      <div class="user-tools">
        <div class="tools-item" @click="handleToQuestion">
          <img class="tools-icon" src="@/assets/common/icon-question.png" />
          <div class="tools-content">常见问题</div>
        </div>
        <div class="tools-item" @click="handleOpenPDF">
          <img class="tools-icon" src="@/assets/common/icon-doc.png" />
          <div class="tools-content">用户手册</div>
        </div>
      </div>
    </div>
    <div class="column-box">
      <div class="column-title">我的数据</div>
      <div class="tools-box">
        <div class="tools-item" v-for="(item, index) in toolList" :key="index">
          <div class="tools-icon">
            <img :src="item.icon" class="icon-img" alt="" />
          </div>
          <div class="tools-content">
            <div class="tools-title">{{ item.name }}</div>
            <div class="tools-value">{{ item.value }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="column-box">
      <div class="column-title">常用功能</div>
      <div class="list-box">
        <div class="list-item" v-for="(item, index) in listData" :key="index" :style="{
          background: 'url(' + item.icon + ') no-repeat center',
          backgroundSize: '100%',
        }" @click="handleFunctionClick(item)">
          <div class="list-img">
            <!-- <img :src="item.icon" class="icon-img" alt="" /> -->
          </div>
          <div class="list-title">{{ item.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted ,computed} from "vue";
import { useRouter } from "vue-router";
import API from "@/api";
const avatar_url = computed(() =>  JSON.parse(localStorage.getItem("userInfo") as any)?.avatar_url) || "./data/user-avatar.jpeg";
const router = useRouter();

const userRealName = ref("李元芳");
const toolList = ref([
  {
    name: "新增维修",
    icon: "./images/operation/home-tool-1.png",
    value: 0,
  },
  {
    name: "报修率",
    icon: "./images/operation/home-tool-2.png",
    value: "0.12%",
  },
  {
    name: "经销商",
    icon: "./images/operation/home-tool-3.png",
    value: 0,
  },
  {
    name: "客户",
    icon: "./images/operation/home-tool-4.png",
    value: 0,
  },
  {
    name: "订单",
    icon: "./images/operation/home-tool-5.png",
    value: 0,
  },
]);
const handleToQuestion = () => {
  router.push("/question");
}
const handleOpenPDF = () => {
  window.open("https://lib.fantasy-lab.com/api/StudentExperiment/PdfForStudentExperiment?student_experiment_id=5d917fbb9bd141f98871f1976f794a7a", "_blank");
}
// 获取首页数据
const fetchDashboardData = async () => {
  try {
    const response = await API.apiDashboardAdminHomeDashboardPost();
    if (response && response.data) {
      const data = response.data;

      // 更新用户姓名
      if (data.user_real_name) {
        userRealName.value = data.user_real_name;
      }

      // 更新数据展示
      toolList.value[0].value = data.new_repair_count || 0;
      toolList.value[1].value = data.repair_rate ? `${data.repair_rate}%` : "0%";
      toolList.value[2].value = data.delaer_count || 0;
      toolList.value[3].value = data.customer_count || 0;
      toolList.value[4].value = data.order_count || 0;
    }
  } catch (error) {
    console.error("获取首页数据失败:", error);
  }
};

onMounted(() => {
  fetchDashboardData();
});
const listData = ref([
  { name: "数据展板", icon: "./images/operation/icon-home-1.png", route: "screen-data" },
  { name: "校本实验", icon: "./images/operation/icon-home-2.png", route: "experiment-management" },
  { name: "器材管理", icon: "./images/operation/icon-home-3.png", route: "equipment-equipment" },
  { name: "教师管理", icon: "./images/operation/icon-home-4.png", route: "administrator-management" },
  // { name: "直播会议", icon: "./images/operation/icon-home-5.png", route: "" }
]);

// 处理常用功能点击事件
const handleFunctionClick = (item: any) => {
  if (item.route) {
    router.push(`/${item.route}`);
  } else {
    console.log(`${item.name} 功能暂未开放`);
  }
};
</script>
<style lang="less" scoped>
.home-page {
  padding: 0 16px;
  min-height: 100%;

  .user-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 25px 0;
    border-bottom: 1px solid #e6ebf1;
    flex-wrap: wrap;

    .user-info {
      display: flex;
      align-items: center;

      @media screen and (max-width: 768px) {
        margin-bottom: 25px;
      }

      .user-avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: #ccc;
        margin-right: 10px;
      }

      .info-box {
        .info-title {
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 18px;
          color: #262626;
          line-height: 26px;
          text-align: left;
          font-style: normal;
          margin-bottom: 3px;
        }

        .info-desc {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #595959;
          line-height: 20px;
          text-align: left;
          font-style: normal;
        }
      }
    }

    .user-tools {
      display: flex;
      align-items: center;

      .tools-item {
        width: 116px;
        height: 46px;
        cursor: pointer;
        background: linear-gradient(270deg,
            rgba(39, 189, 247, 0.1) 0%,
            rgba(47, 125, 251, 0.1) 100%);
        border-radius: 8px;
        margin-left: 24px;
        display: flex;
        align-items: center;
        justify-content: center;

        &:last-child {
          background: linear-gradient(270deg,
              rgba(253, 164, 36, 0.1) 0%,
              rgba(253, 117, 58, 0.1) 100%);
        }

        .tools-icon {
          width: 23px;
          height: 22px;
          margin-right: 14px;
        }

        .tools-content {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #262626;
          line-height: 20px;
          text-align: left;
          font-style: normal;
        }
      }
    }

    .user-menu {
      height: 40px;

      margin-bottom: 25px;
      background: #ffffff;
      border: 1px solid #bec4ca;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 8px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 14px;
      cursor: pointer;
      color: #262626;

      .icon-link {
        width: 16px;
        height: 16px;
        background: url("@/assets/icon-link.png") no-repeat;

        margin-left: 5px;
      }
    }
  }

  .column-box {
    margin-top: 14px;

    .column-title {
      padding: 16px 0;
      font-family: PingFangSC-Medium;
      font-weight: 500;
      font-size: 16px;
      color: #262626;
      line-height: 22px;
    }
  }

  .tools-box {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .tools-item {
      width: 216px;
      padding: 18px 0;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      position: relative;

      @media screen and (max-width: 768px) {
        width: 50%;
        flex: auto;
        margin-bottom: 12px;
      }

      &::after {
        position: absolute;
        right: 0;
        content: "";
        top: 18px;
        bottom: 18px;
        width: 1px;
        box-shadow: inset -1px 0 0px 0px #e6ebf1;
      }

      &:last-child {
        &::after {
          display: none;
        }
      }

      .tools-icon {
        width: 46px;
        height: 46px;
        margin-right: 12px;

        .icon-img {
          width: 46px;
          height: 46px;
        }
      }

      .tools-content {
        .tools-title {
          font-family: PingFangSC-Regular;
          font-weight: 400;
          font-size: 14px;
          line-height: 20px;
          color: #262626;
        }

        .tools-value {
          font-family: PingFangSC-Medium;
          font-weight: 500;
          font-size: 18px;
          color: #262626;
          letter-spacing: 0;
          line-height: 26px;
        }
      }
    }
  }

  .list-box {
    display: flex;
    flex-wrap: wrap;
    margin: 0 -12px;

    @media screen and (max-width: 768px) {
      justify-content: center;
    }

    .list-item {
      width: 200px;
      height: 250px;
      background-color: #e6f5ff;
      background-position: center center;
      background-repeat: no-repeat;
      border-radius: 12px;
      box-sizing: border-box;
      padding: 12px;
      cursor: pointer;
      margin: 0 12px 23px 12px;
      transition: transform 0.2s ease, box-shadow 0.2s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }

      .list-img {
        height: 180px;
        margin-bottom: 12px;

        .icon-img {
          width: 176px;
          height: 180px;
        }
      }

      .list-title {
        width: 176px;
        height: 46px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-top: 1px solid #2626260f;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        font-size: 14px;
        color: #262626;
        text-align: center;
      }
    }
  }
}
</style>
